﻿Public Class ListInOut1

    Private Sub LoadData()
        Dim dt As New DataTable
        Dim dc As DataColumn
        dt.Columns.Add("月份")
        dc = dt.Columns.Add("收入", GetType(Decimal))
        dc.DefaultValue = 0

        dt.Columns.Add("支出", GetType(Decimal))
        dt.Columns.Add("统计", GetType(Decimal))

        '抽数据
        Dim Year As Integer = Me.dtFindDate.Value.Year
        Dim endmouth As Integer = CInt(Me.cboMouth.Text)

        Dim strSql As String = String.Empty
        Dim strSql1 As String = String.Empty
        Dim dr As DataRow

        For day As Integer = 1 To DateTime.DaysInMonth(Year, endmouth)
            Dim strBegin As String = Year.ToString("0000") + "-" + endmouth.ToString("00") + "-" + day.ToString("00")
            strSql = "select  sum(count) as count from WysInput where date([DateTime])='" + strBegin + "'"
            strSql1 = "select  sum(count) as count from WysOutput where date([DateTime])='" + strBegin + "'"

            dr = dt.NewRow
            dr(0) = day.ToString & "日"
            Dim dt2 As DataTable = ct.FillTable(strSql)
            If dt2 IsNot Nothing AndAlso dt2.Rows.Count > 0 Then
                dr(1) = 结果(dt2.Rows(0)(0))
            Else
                dr(1) = 0
            End If
            dt2 = ct.FillTable(strSql1)
            If dt2 IsNot Nothing AndAlso dt2.Rows.Count > 0 Then
                dr(2) = 结果(dt2.Rows(0)(0))
            Else
                dr(2) = 0
            End If
            dr(3) = CDec(dr(1)) - CDec(dr(2))
            dt.Rows.Add(dr)
        Next

        dr = dt.NewRow
        dr(1) = 0
        dr(2) = 0
        dr(3) = 0

        dr(0) = "合计"
        For i As Integer = 0 To dt.Rows.Count - 1
            dr(1) += 结果(dt.Rows(i)(1))
            dr(2) += 结果(dt.Rows(i)(2))
            dr(3) += 结果(dt.Rows(i)(3))
        Next
        dt.Rows.Add(dr)
        Me.DataGridView1.DataSource = dt
    End Sub

    Private Sub ListInOut_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.cboMouth.SelectedIndex = (Now.Month - 1).ToString
        LoadData()
    End Sub

    Public Function 结果(ByVal ob As Object) As Decimal
        If ob Is Nothing Then
            Return 0
        End If
        If ob Is DBNull.Value Then
            ob = 0
        End If
        Return CDec(ob)
    End Function

    Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
        dtFindDate.Value = dtFindDate.Value.AddYears(1)
        'LoadData()
    End Sub

    Private Sub btnPre_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPre.Click
        dtFindDate.Value = dtFindDate.Value.AddYears(-1)
        'LoadData()
    End Sub

    Private Sub cboMouth_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboMouth.SelectedIndexChanged
        LoadData()
    End Sub

    Private Sub btnMonthPre_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMonthPre.Click
        If cboMouth.Items.Count > 0 Then
            If cboMouth.SelectedIndex < 1 Then
                cboMouth.SelectedIndex = 0
            Else
                cboMouth.SelectedIndex -= 1
            End If
            'LoadData()
        End If

    End Sub

    Private Sub btnMonthNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMonthNext.Click
        If cboMouth.Items.Count > 0 Then
            If cboMouth.SelectedIndex >= cboMouth.Items.Count - 1 Then
                cboMouth.SelectedIndex = cboMouth.Items.Count - 1
            Else
                cboMouth.SelectedIndex += 1
            End If
            'LoadData()
        End If
    End Sub

    Private Sub dtFindDate_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dtFindDate.ValueChanged
        LoadData()
    End Sub
End Class
